package androidlab.serverclasses;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;


public class MobileUser {
	
	public int ID;
	public File gpxFile;
	boolean isFirstTime = true;
	private double[] initialPosition;
	private double[] currentPosition;
	private double[] predictionVector;
	private final SimpleDateFormat pointDateFormatter = new SimpleDateFormat("yyyy-MM-dd 'T' HH:mm:ss'Z'");

	private final String xmlHeader = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";
	private final String gpxTag = "<gpx"
	        + " xmlns=\"http://www.topografix.com/GPX/1/1\""
	        + " version=\"1.1\""
	        + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
	        + " xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd \">";
	private final String metaDataTag = "<metadata>"+

		"\n<time>"+pointDateFormatter.format(new Date())+"</time>"+
		"\n<bounds minlat=\"48.469001667\" minlon=\"9.000000000\" maxlat=\"49.000000000\" maxlon=\"9.999900000\"/>"+
		"\n</metadata> ";

	private String lastLocation;
	
	public MobileUser(int ID){
		this.ID = ID;
		gpxFile = new File(this.ID+".gpx");
	}
	
	public void setCurrentPosition(double[] currentPosition){
		this.currentPosition = currentPosition;
		//Longitude,Latitude,Time
		this.lastLocation = currentPosition[0]+","+currentPosition[1]+","+currentPosition[2];
	}
	
	public double[] getCurrentPosition(){
		return this.currentPosition;
	}
	
	// Called the first time a user sends its location.
	public void createGPXFile(){
		try {
	
			gpxFile.createNewFile();

			FileWriter gpxWriter = new FileWriter(gpxFile);
			
			BufferedWriter bfw = new BufferedWriter(gpxWriter);
			bfw.write(xmlHeader+"\n");
			bfw.write(gpxTag+"\n");
			bfw.write(metaDataTag+"\n");
			bfw.write("<trk>"+"\n");
			bfw.write("<trkseg>"+"\n");
			String trackPointTag="<trkpt lat=\""+getInitialPostion()[0]+"\" lon=\""+getInitialPostion()[1]+"\">"+
			"    \n<time>"+getInitialPostion()[2]+"</time>\n"+
			"</trkpt>\n";

			bfw.write(trackPointTag+"\n");
			bfw.close();
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void updateGpxFile(){

		try {
			FileWriter gpxWriter; 

			gpxWriter = new FileWriter(gpxFile,true);
			

			BufferedWriter out = new BufferedWriter(gpxWriter);

			String trackPointTag="<trkpt lat=\""+this.currentPosition[0]+"\" lon=\""+this.currentPosition[1]+"\">"+
			"    \n<time>"+this.currentPosition[2]+"</time>\n"+
			"</trkpt>\n";

			out.write(trackPointTag+"\n");

			out.close();
		}
		catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void endGPXFile(){
		try {
			FileWriter gpxWriter = new FileWriter(gpxFile,true);
			BufferedWriter out = new BufferedWriter(gpxWriter);
			
			out.write("</trkseg>"+"\n");
			out.write("</trk>"+"\n");
			out.write("</gpx>");
			
			out.close();
			gpxWriter.close();
			
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public String getLastLocation(){
		return this.lastLocation;
	}
	public double[] getInitialPostion(){
		return this.initialPosition;
	}
	public void setInitialPosition(double[] initialPosition){
		this.initialPosition = initialPosition;
		setCurrentPosition(this.initialPosition);
		createGPXFile();
	}

	public void setPredictionVector(double[] predictionVector) {
		this.predictionVector = predictionVector;
	}

	public double[] getPredictionVector() {
		return predictionVector;
	}
	
}